package practice1_100;

import java.util.HashMap;
import java.util.Map;

public class Practice3 {
    public int lengthOfLongestSubstring(String s) {
//
        //滑动窗口解法
        int left=0;
        int right=0;
        //
        int maxlength=0;
        Map<Character,Integer> map=new HashMap<>();
        while(s.length()>right){
            char a=s.charAt(right);
            if(map.get(a)!=null){
                maxlength=maxlength>map.size()?maxlength:map.size();//获取最长字串长度
               //获得重复字符的定位,删除所有的其他值
                int current=map.get(a);
                while (left<=current){
                    map.remove(s.charAt(left));
                    left++;
                }
                map.put(a,right);
            }else {
                map.put(a,right);
                maxlength=maxlength>map.size()?maxlength:map.size();//获取最长字串长度
            }
            right++;
        }
        return maxlength;
    }

    public static void main(String[] args) {
        Practice3 practice3=new Practice3();
       int a= practice3.lengthOfLongestSubstring("pwwtyjihiiuuiiopkew");
        System.out.println(a);
    }
}
